﻿using System.Windows.Input;
using MahApps.Metro.Controls;
using Microsoft.Tools.WindowsInstallerXml.Bootstrapper;
using Wix.Win8SetupBootstrapper.Classes;
using Wix.Win8SetupBootstrapper.Enums;

namespace Wix.Win8SetupBootstrapper.ViewModels
{
    public abstract class PageViewModelBase : ViewModelBase
    {
        private readonly RootViewModel _rootViewModel;
        private string _backButtonText;
        private bool _backButtonVisible;
        private bool _canBack;
        private bool _canCancel;
        private bool _canNext;
        private string _cancelButtonText;
        private bool _cancelButtonVisible;
        private string _nextButtonText;
        private bool _nextButtonVisible;

        protected PageViewModelBase(RootViewModel rootViewModel)
            : base(rootViewModel.Bootstrapper)
        {
            _rootViewModel = rootViewModel;
            _rootViewModel.Bootstrapper.PlanComplete += Bootstrapper_PlanComplete;

            if (_rootViewModel.PageController.NextPage == Pages.ProgressPage)
            {
                NextButtonText = "Install";
            }
            else
            {
                NextButtonText = "Next";
            }

            BackButtonText = "Back";
            CancelButtonText = "Cancel";

            this.CancelButtonVisible = true;
            this.BackButtonVisible = true;
            this.NextButtonVisible = true;
        }

        public RootViewModel RootViewModel
        {
            get { return _rootViewModel; }
        }

        public virtual ICommand CancelCommand
        {
            get
            {
                return new BaseCommand(delegate { _rootViewModel.RequestCancel(true); }, delegate { return CanCancel; });
            }
        }

        public virtual ICommand NextCommand
        {
            get { return new BaseCommand(delegate { NextPage(); }, delegate { return CanNext; }); }
        }

        public virtual ICommand BackCommand
        {
            get { return new BaseCommand(delegate { PreviousPage(); }, delegate { return CanBack; }); }
        }


        public string CancelButtonText
        {
            get { return _cancelButtonText; }
            protected set
            {
                _cancelButtonText = value;
                OnPropertyChanged();
            }
        }

        public string NextButtonText
        {
            get { return _nextButtonText; }
            protected set
            {
                _nextButtonText = value;
                OnPropertyChanged();
            }
        }

        public string BackButtonText
        {
            get { return _backButtonText; }
            protected set
            {
                _backButtonText = value;
                OnPropertyChanged();
            }
        }

        public virtual bool CanCancel
        {
            get { return _canCancel; }
            set
            {
                _canCancel = value;
                OnPropertyChanged();
            }
        }

        public virtual bool CanBack
        {
            get { return _canBack; }
            set
            {
                _canBack = value;
                OnPropertyChanged();
            }
        }

        public virtual bool CanNext
        {
            get { return _canNext; }
            set
            {
                _canNext = value;
                OnPropertyChanged();
            }
        }

        public bool CancelButtonVisible
        {
            get { return _cancelButtonVisible; }
            set
            {
                _cancelButtonVisible = value;
                OnPropertyChanged();
            }
        }

        public bool NextButtonVisible
        {
            get { return _nextButtonVisible; }
            set
            {
                _nextButtonVisible = value;
                OnPropertyChanged();
            }
        }

        public bool BackButtonVisible
        {
            get { return _backButtonVisible; }
            set
            {
                _backButtonVisible = value;
                OnPropertyChanged();
            }
        }

        private void Bootstrapper_PlanComplete(object sender, PlanCompleteEventArgs e)
        {
            _rootViewModel.ApplyAction();
        }

        private void NextPage()
        {
            _rootViewModel.TransitionType = TransitionType.Left;

            _rootViewModel.SetCurrentPage(_rootViewModel.PageController.NextPage);
        }

        private void PreviousPage()
        {
            if (_rootViewModel.PageController.PreviousPage == Pages.None ||
                _rootViewModel.PageController.PreviousPage == Pages.ProgressPage)
            {
                _rootViewModel.RequestCancel(true);
            }
            _rootViewModel.TransitionType = TransitionType.Right;
            _rootViewModel.SetCurrentPage(_rootViewModel.PageController.PreviousPage);
        }
    }
}